package io.renren.handler;

import io.renren.entity.WxMessageLog;
import io.renren.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Slf4j
@Component
public class LogHandler extends AbstractHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) {
        //保存消息内容到数据库
        WxMessageLog wxMessageLog = new WxMessageLog();
        wxMessageLog.setStatus("0");
        wxMessageLog.setCreateTime(new Date());
        wxMessageLog.setEvent(wxMessage.getEvent());
        wxMessageLog.setFormat(wxMessage.getFormat());
        wxMessageLog.setPicUrl(wxMessage.getPicUrl());
        wxMessageLog.setTicket(wxMessage.getTicket());
        wxMessageLog.setMsgId(wxMessage.getMsgId() + "");
        wxMessageLog.setMediaId(wxMessage.getMediaId());
        wxMessageLog.setMsgType(wxMessage.getMsgType());
        wxMessageLog.setContent(wxMessage.getContent());
        wxMessageLog.setEventKey(wxMessage.getEventKey());
        wxMessageLog.setFromUserName(wxMessage.getFromUser());
        wxMessageLog.setRecognition(wxMessage.getRecognition());
        wxMessageLog.setToUserName(wxMessage.getToUser());

        log.info(JsonUtils.toJson(wxMessageLog));
        return null;
    }

}
